class NewsController {
    

     static roleAccess =  [
            [action:["*"], roles:["admin"]],
            [action:["list","index","read"], roles:["*"]],
	]

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ newsList: News.list( params ) ]
    }

    def show = {
        def news = News.get( params.id )

        if(!news) {
            flash.message = "News not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ news : news ] }
    }

    def delete = {
        def news = News.get( params.id )
        if(news) {
            news.delete()
            flash.message = "News ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "News not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def news = News.get( params.id )

        if(!news) {
            flash.message = "News not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ news : news ]
        }
    }

    def update = {
        def news = News.get( params.id )
        if(news) {
            news.properties = params

		params.findAll{pm-> 
			pm.key.length()>2 && pm.key[0..2]=="otm"
		}.each {  p->
			def keys = p.key.split("_");
			def classkey = keys[1]
			def addtokeyorig = keys[2]
			def addtokey = 'addTo' + addtokeyorig[0].toUpperCase() + addtokeyorig[1..-1]
			classkey = classkey[0].toUpperCase() + classkey[1..-1]
		
			news."$addtokeyorig"= null

			p.value.each { val->
				def temp = Class.forName(classkey, true, grailsApplication.classLoader).newInstance()
				temp = temp.get(val)
				news."$addtokey"(temp)			
			} 
		}


            if(!news.hasErrors() && news.save()) {
                flash.message = "News ${params.id} updated"
                redirect(action:show,id:news.id)
            }
            else {
                render(view:'edit',model:[news:news])
            }
        }
        else {
            flash.message = "News not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def news = new News()
        news.properties = params
        return ['news':news]
    }

    def save = {
        def news = new News(params)


	params.findAll{pm-> 
		pm.key.length()>2 && pm.key[0..2]=="otm"
	}.each {  p->
		def keys = p.key.split("_");
		def classkey = keys[1]
		def addtokeyorig = keys[2]
		def addtokey = 'addTo' + addtokeyorig[0].toUpperCase() + addtokeyorig[1..-1]
		classkey = classkey[0].toUpperCase() + classkey[1..-1]
	
		News."$addtokeyorig"= null

		p.value.each { val->
			def temp = Class.forName(classkey, true, grailsApplication.classLoader).newInstance()
			temp = temp.get(val)
			News."$addtokey"(temp)			
		} 
	}


        if(!news.hasErrors() && news.save()) {
            flash.message = "News ${news.id} created"
            redirect(action:show,id:news.id)
        }
        else {
            render(view:'create',model:[news:news])
        }
    }



/**
 * read the given article
 * let's do some permission checking on it to see if we are allowed to read it
 */
    def read = {

        def news = News.get( params.id )

        if(!news) {
            flash.message = "News not found with id ${params.id}"
            redirect(action:list)
        }

        def hasMatch = false
        if(session?.user && session?.user?.roles) {
                session.user.roles.each { r->
                        news.roles.each { nr ->
                                if(nr.toString()==r.toString()) {
                                        hasMatch = true
                                }
                        }
                }

        }

        if(hasMatch) {
                return  [news:news]
        } else {
                return [news:null]
        }
    }
    

}
